from flask import Blueprint, render_template, request, session
from model.article import Article
# from model.browse import BrowseHistory
from model.user import User
from common.database import db_connect

detail = Blueprint("detail", __name__)


@detail.route("/detail")
def article_detail():
    article_id = request.args.get("article_id")

    # 记录浏览历史（仅限登录用户）
    # if session.get("is_login") == "true":
    #     user_id = session.get("user_id")
    #     new_browse = BrowseHistory(
    #         user_id=user_id,
    #         article_id=article_id
    #     )
    #     db_session.add(new_browse)
    #     db_session.commit()

    # 获取文章详情
    db_session, Base, engine = db_connect()
    article = db_session.query(Article).filter(Article.id == article_id).first()

    # 更新浏览数
    if article:
        article.browse_num += 1
        db_session.commit()

    # 获取作者信息
    author = db_session.query(User).filter(User.user_id == article.user_id).first()

    return render_template("detail.html",
                           article=article,
                           author=author)
    return render_template("detail.html", article=article, author=author)